package vue;

import bdd.DAOOptions;
import java.awt.CardLayout;
import static java.awt.Component.CENTER_ALIGNMENT;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.awt.image.ImageObserver.WIDTH;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import modele.Jeu1;

/**
 *
 * @author flo
 */
public class Fenetre2 extends JFrame {

    private Jeu1 j1;
    private VueJeu1 v;
    final CardLayout cl = new CardLayout();
    JPanel content = new JPanel();
    String[] listContent = {"acceuil", "jeux", "param", "scores", "vuejeu"};
    int indice = 0;

    public Fenetre2() throws SQLException {
        j1 = new Jeu1(10);
        v = new VueJeu1();
        j1.setVue(v);
        this.setTitle("ENGLISH NOT NULL EXCEPTION");
        this.setSize(600, 300);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.creerFenetre();

    }

    public VueJeu1 getVue() {
        return v;
    }

    private void creerFenetre() throws SQLException {


        /*Panneau d'acceuil*/
        JPanel card1 = new MainMenuPanel();
        JButton jeux = new JButton("Start Game");
        JButton scores = new JButton("High Scores");
        JButton param = new JButton("Settings");
        JButton quitter = new JButton("Exit");
        this.v = new VueJeu1();
        jeux.setAlignmentX(CENTER_ALIGNMENT);
        scores.setAlignmentX(CENTER_ALIGNMENT);
        param.setAlignmentX(CENTER_ALIGNMENT);
        quitter.setAlignmentX(CENTER_ALIGNMENT);
        card1.add(jeux);
        card1.add(scores);
        card1.add(param);
        card1.add(quitter);

        /* Choix des jeux */
        JPanel card2 = new GameChoicePanel();
        JButton retour = new JButton("Return");
        JButton jeu1 = new JButton("Game 1");
        JButton jeu2 = new JButton("Game 2");
        jeu1.setAlignmentX(CENTER_ALIGNMENT);
        jeu2.setAlignmentX(CENTER_ALIGNMENT);
        retour.setAlignmentX(CENTER_ALIGNMENT);
        card2.add(jeu1);
        card2.add(jeu2);
        card2.add(retour);

        /*Parametres */
        JPanel card3 = new SettingsPanel();
        JButton retour2 = new JButton("Return");
        JButton duree1 = new JButton("30 seconds");
        JButton duree2 = new JButton("60 seconds");
        duree1.setAlignmentX(CENTER_ALIGNMENT);
        duree2.setAlignmentX(CENTER_ALIGNMENT);
        retour2.setAlignmentX(CENTER_ALIGNMENT);
        card3.add(duree1);
        card3.add(duree2);
        card3.add(retour2);

        /*Meilleurs scores */
        JPanel card4 = new HighScorePanel();
        VueScores vuescores =new VueScores();
        JButton retour3 = new JButton("Return");
        vuescores.setAlignmentX(CENTER_ALIGNMENT);
        retour3.setAlignmentX(CENTER_ALIGNMENT);
        
        card4.add(vuescores);
        card4.add(retour3);

        content.setLayout(cl);
        content.add(card1, listContent[0]);
        content.add(card2, listContent[1]);
        content.add(card3, listContent[2]);
        content.add(card4, listContent[3]);
        content.add(this.v, listContent[4]);
        this.setContentPane(content);
        this.repaint();
        this.revalidate();

         jeux.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Fenetre2.this.cl.show(content, "jeux");
            }
        });

        param.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Fenetre2.this.cl.show(content, "param");
            }
        });

        retour.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Fenetre2.this.cl.show(content, "acceuil");
            }
        });

        retour2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
                Fenetre2.this.cl.show(content, "acceuil");
                
            }
        });

        retour3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Fenetre2.this.cl.show(content, "acceuil");
            }
        });
        
        jeu2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(Fenetre2.this.getContentPane(), "The game is in development !","Available soon",WIDTH);
            }
        });

        jeu1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Rectangle r = Fenetre2.this.getBounds();

                ThreadVue tv = new ThreadVue(r);
                tv.start();
                Fenetre2.this.dispose();
                System.gc();
            }
        });

        scores.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Fenetre2.this.cl.show(content, "scores");
            }
        });
        
        duree1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new DAOOptions().setTemps(30);
                } catch (SQLException ex) {
                    Logger.getLogger(Fenetre2.class.getName()).log(Level.SEVERE, null, ex);
                }
                Fenetre2.this.cl.show(content, "acceuil");
                JOptionPane.showMessageDialog(Fenetre2.this.getContentPane(), "The time is 30 seconds","Settings",WIDTH);
            }
        });
        
        duree2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new DAOOptions().setTemps(60);
                } catch (SQLException ex) {
                    Logger.getLogger(Fenetre2.class.getName()).log(Level.SEVERE, null, ex);
                }
                Fenetre2.this.cl.show(content, "acceuil");
                JOptionPane.showMessageDialog(Fenetre2.this.getContentPane(), "The time is 60 seconds","Settings",WIDTH);
            }
        });

        quitter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

    }
    
    public Fenetre2(Rectangle r) throws SQLException{
        this();
        this.setBounds(r);
        this.repaint();
        this.revalidate();
    }

    public void lancerjeu() throws SQLException {
        this.j1.run();
    }

}
